package com.zhanyurui.provider.controller;

import com.zhanyurui.provider.domain.TestDomain;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description TODO
 * @Date 2024/8/6 9:42
 * @Created by 10574
 */
@RestController
@RequestMapping("test")
public class HelloController {

    @RequestMapping("hello")
    public String hello() {
        return "hello!!!!";
    }

    /**
     * get请求RequestBody,也能处理，requestBody只关注请求体中是否有内容，不关注是什么类型的请求
     */
    @GetMapping("getRequestBody")
    public String getRequestBody(@RequestBody(required = false) TestDomain testDomain) {
        System.out.println("name:" + testDomain.getName());
        System.out.println("age:" + testDomain.getAge());
        testDomain.setDescription("getRequestBody");
        return testDomain.toString();
    }

    /**
     * get请求RequestBody,也能处理，requestBody只关注请求体中是否有内容，不关注是什么类型的请求
     */
    @PostMapping("postRequestBody")
    public String postRequestBody(@RequestBody(required = false) TestDomain testDomain) {
        System.out.println("name:" + testDomain.getName());
        System.out.println("age:" + testDomain.getAge());
        testDomain.setDescription("postRequestBody");
        return testDomain.toString();
    }

    /**
     * RequestPart处理文件、json文件(解析成对象)、和其他单个参数
     */
    @GetMapping("getRequestPart")
    public String requestPartJsonFile(@RequestPart MultipartFile file, @RequestPart TestDomain testDomain, @RequestPart String age) {
        System.out.println("file name: " + file.getOriginalFilename());
        System.out.println("testDomain: " + testDomain.toString());
        System.out.println("age: " + age);
        return "getRequestPart";
    }

    /**
     * RequestPart处理文件、json文件(解析成对象)、和其他单个参数
     */
    @PostMapping("postRequestPart")
    public String postRequestPart(@RequestPart MultipartFile file, @RequestPart TestDomain testDomain, @RequestPart String age) {
        System.out.println("file name: " + file.getOriginalFilename());
        System.out.println("testDomain: " + testDomain.toString());
        System.out.println("age: " + age);
        return "postRequestPart";
    }

    /**
     * 测试post请求读取url上的参数
     */
    @PostMapping("postUrlParam")
    public String postUrlParam(@RequestParam String name, @RequestParam Integer age) {
        return name + ":" + age;
    }

    /**
     * 绑定 URL 查询参数或表单数据到方法参数。也可支持文件上传
     */
    @PostMapping("postRequestParam")
    public String postRequestParam(@RequestParam String name, @RequestParam MultipartFile file){
        System.out.println("file name: " + file.getOriginalFilename());
        System.out.println("name: " + name);
        return "postRequestParam";
    }
}
